package com.syni.mdd.yhd.common.all.controller.protect;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsAgentDao;
import com.syni.mdd.yhd.common.all.entity.BmsAgent;
import com.syni.mdd.yhd.common.all.entity.ImgPrefix;
import com.syni.mdd.yhd.common.all.entity.activity.BmsMealPrize;
import com.syni.mdd.yhd.common.all.service.BmsAgentService;
import com.syni.mdd.yhd.common.all.service.ImgPrefixService;
import com.syni.mdd.yhd.common.all.service.activity.BmsMealPrizeService;
import com.syni.mdd.yhd.common.all.utils.ParamData;
import com.syni.mdd.yhd.common.all.utils.RequestParamUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @className TestController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/22 11:02
 * @VERSION 1.0
 **/
@Controller
@RequestMapping("/test")
@CrossOrigin
public class TestController {

    @Autowired
    BmsMealPrizeService bmsMealPrizeService;

    @Autowired
    EntityManager entityManager;

    @Autowired
    ImgPrefixService imgPrefixService;

    AtomicInteger atomicInteger = new AtomicInteger();

    @Autowired
    BmsAgentService bmsAgentService;

    @Autowired
    BmsAgentDao bmsAgentDao;

    @Autowired(required = false)
    com.syni.mdd.yhd.common.all.twosource.BmsAgentDao bmsAgentDao2;


    @GetMapping("testImg")
    @ResponseBody
    public Boolean testImg(@RequestBody  ParamsMap paramsMap, HttpServletRequest request){

        String code = request.getParameter("code");
        System.out.println(code);
        ImgPrefix imgPrefix = new ImgPrefix();
        imgPrefix.setRemark("123");
        imgPrefix.setPrefix("/123wqe");
        List<ImgPrefix> imgPrefixes = new ArrayList<>();
        imgPrefixes.add(imgPrefix);
        ImgPrefix imgPrefix2 = new ImgPrefix();
        imgPrefix2.setRemark("12322");
        imgPrefix2.setPrefix("http://www.baiu.com/1/2/123wqe");
        imgPrefixes.add(imgPrefix2);
        imgPrefixService.pushSpliceField(imgPrefixes,"prefix");
        System.out.println(imgPrefixes);
        return true;
    }

    @GetMapping("/getImg")
    @ResponseBody
    public boolean getImg(HttpServletResponse response) throws IOException {
        /*File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "static/01.jpg");
        String imgPathEncode = "static/01.png";
        response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
        String imgPath = URLDecoder.decode(imgPathEncode, "utf-8");
        System.out.println(file.getPath());
        System.out.println(file.getAbsolutePath());*/
        String imgPath = "static/01.jpg";
        //OutImageUtils.outImg(imgPath,response);
        return true;

    }

    @PostMapping("/testApiAuth")
    @ResponseBody
    public Map<String,Object> testApiAuth(@RequestBody ParamsMap paramsMap){
        return ResultMapHelper.success(null,"ok");
    }

    /*@PostMapping("/signed")
    @ResponseBody
    public String testSigned(HttpServletRequest request){
        //request.get
        ParamData  paramsMap = new ParamData();
        System.out.println("前端参数： " + JSON.toJSONString(paramsMap));
        return "success";
    }

    @GetMapping("/signed")
    @ResponseBody
    public String testSignedGet(){
        ParamData  paramsMap = new ParamData();
        System.out.println("前端参数： " + JSON.toJSONString(paramsMap));
        return "success";
    }*/

    @GetMapping("/signed")
    @ResponseBody
    public String testSignedGet(){
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        ParamData  paramsMap = new ParamData();
        System.out.println(RequestParamUtils.getRequest().getRemoteUser());
        System.out.println(RequestParamUtils.getRequest().getRemoteAddr());
        System.out.println(requestHeaderData.getString("x-forwarded-for"));
        return "success";
    }

    @GetMapping("/cache")
    @ResponseBody
    public String cache(){
        BmsMealPrize bmsMealPrize = bmsMealPrizeService.findPrizeById(1);
        return JSON.toJSONString(bmsMealPrize);
    }

    @GetMapping("/userQuartz")
    @ResponseBody
    public String userQuartz(){
        BmsAgent bmsAgents = bmsAgentService.getBaseDao().findByAgentId("11");
        BmsAgent bmsAgentss = bmsAgentDao2.findByAgentId("11");
        System.out.println(bmsAgents);
        System.out.println(bmsAgentss);
        return "success";
    }

}

